var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
			hide : true,
		}, {
			colkey : "mName",
			name : "用户名",
		}, {
			colkey : "mSex",
			name : "性别",
			renderData : function(rowindex,data, rowdata, column) {
				if(data==1){
					return '女';
				}else{
					return '男';
				}
			}
		}, {
			colkey : "mPhone",
			name:"用户电话",
		}, {
			colkey : "companyName",
			name : "代理商公司",
		}, {
			colkey : "linkman",
			name : "代理联系人",
		}, {
			colkey : "phone",
			name : "代理商电话",
		}, {
			colkey : "gradeName",
			name : "代理商等级",
		}, {
			colkey : "createTime",
			name : "创建时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd");
			}
		}, {
			colkey : "reviewStatus",
			name : "审核状态",
			renderData : function( rowindex ,data, rowdata, colkeyn) {
				var state="";
				if(data==0){
					state="<span style='color:green'>待审核</span>"
				}else if(data==1){
					state="<span style='color:green'>通过审核</span>"
				}else if(data==2){
					state="<span style='color:red'>未通过</span>"
				}else{
					state="<span style='color:blue'>已有更高等级</span>"
				}
				return state;
			}
		} ],
		jsonUrl : rootPath + '/agent/selectList',
		checkbox : true,
		checkValue : "id"
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#reviewAgent").click("click", function() {
		reviewAgent();
	});
	$("#editAgent").click("click", function() {
		editAgent();
	});
	$("#deleteAgent").click("click", function() {
		deleteAgent();
	});
});
function reviewAgent() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1) {
		layer.msg("只能选中一个");
		return;
	}
	if (cbox.length == 0) {
		layer.msg("请选择要审核的代理");
		return;
	}
	pageii = layer.open({
		title : "审核",
		type : 2,
		area : [ "500px", "85%" ],
		content : rootPath + '/agent/reviewAgent?id=' + cbox
	});
}

function editAgent() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1) {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "500px", "75%" ],
		content : rootPath + '/agent/addOrEdit?id=' + cbox
	});
}

function deleteAgent() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/agent/deleteEntity';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
